#!/bin/bash
#
# $Id: post-install 3705 2013-08-07 19:47:20Z dhill $
#
# Post-install steps for columnstore install

prefix=/usr/local
installdir=$prefix/mariadb/columnstore
rpmmode=install
user=`whoami 2>/dev/null`

quiet=0

for arg in "$@"; do
	if [ `expr -- "$arg" : '--prefix='` -eq 9 ]; then
		prefix="`echo $arg | awk -F= '{print $2}'`"
		installdir=$prefix/mariadb/columnstore
	elif [ `expr -- "$arg" : '--rpmmode='` -eq 10 ]; then
		rpmmode="`echo $arg | awk -F= '{print $2}'`"
	elif [ `expr -- "$arg" : '--installdir='` -eq 13 ]; then
		installdirTmp="`echo $arg | awk -F= '{print $2}'`"
		if [ $installdirTmp == $installdir ] ; then
			user=root
			export COLUMNSTORE_INSTALL_DIR=$installdir
			export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$installdir/lib:$installdir/mysql/lib
		else
			installdir=$installdirTmp
			prefix=`dirname $installdir`
                prefix=`dirname $prefix`
		fi                
	elif [ `expr -- "$arg" : '--user='` -eq 7 ]; then
		user="`echo $arg | awk -F= '{print $2}'`"
	elif [ `expr -- "$arg" : '--quiet'` -eq 7 ]; then
		quiet=1
	elif [ `expr -- "$arg" : '--plugin='` -eq 9 ]; then
		plugin="`echo $arg | awk -F= '{print $2}'`"
	else
		echo "post-install: invalid unknown argument: $arg" 1>&2
		echo "exiting..."
		exit 1
	fi
done
 
if [ $user != "root" ]; then
	export COLUMNSTORE_INSTALL_DIR=$installdir
	export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$installdir/lib:$installdir/mysql/lib
else
	# add library config file
	/bin/cp -f $installdir/bin/columnstore.conf /etc/ld.so.conf.d/. >/dev/null 2>&1
	ldconfig >/dev/null 2>&1
fi

#check 64-bit OS compatiable
arch=`uname -m`
patcnt=`expr "$arch" : 'i.86'`
is64bitos=1
if [ $patcnt -ne 0 ]; then
	is64bitos=0
fi
is64bitpkg=1
file $installdir/bin/PrimProc | grep '64-bit' >/dev/null 2>&1
if [ $? -ne 0 ]; then
	is64bitpkg=0
fi
if [ $is64bitpkg -eq 1 -a $is64bitos -ne 1 ]; then
	echo "ERROR: Incompatiable Version, package is intended for a x86_64 architecture"
	echo "exiting...."
	exit 1
fi

if [ ! -f $installdir/etc/Columnstore.xml ]; then
	echo "$installdir/etc/Columnstore.xml not found, exiting"
	exit 1
fi

cd $installdir/lib || exit 1

touch /dev/shm/columnstore-test && rm /dev/shm/columnstore-test
if [ $? -ne 0 ] ; then
    echo "User $user will need R/W access to /dev/shm."
    exit 1
fi

#remove libudfsdk.so.1.0.0 file, if it exist # mcol-875
rm -f $installdir/lib/libudfsdk.so
rm -f $installdir/lib/libudfsdk.so.1
rm -f $installdir/lib/libudfsdk.so.1.0.0

for lib in *.so.1.0.0; do
	blib=`basename $lib .1.0.0`
	ln -sf $lib $blib
	ln -sf $lib ${blib}.1
done
for lib in *.so.1.1.0; do
        blib=`basename $lib .1.1.0`
        ln -sf $lib $blib
        ln -sf $lib ${blib}.1
done
chown -h $user.$user *.so >/dev/null 2>&1

if [ -f libstdc++.so.6.0.14 ]; then
	ln -sf libstdc++.so.6.0.14 libstdc++.so.6
	chown -h $user.$user libstdc++.so.6 >/dev/null 2>&1
fi

# softlink for libperl.sp, used by cplogger
ln -s /usr/lib64/perl5/CORE/libperl.so /usr/lib64/libperl.so >/dev/null 2>&1

if [ $installdir != "/usr/local/mariadb/columnstore" ]; then
        sed -i -e s@/usr/local/mariadb/columnstore@$installdir@g $installdir/bin/columnstoreAlias
fi

if [ $user != "root" ]; then
	if [ -f $prefix/.bash_profile ]; then		
		profileFile=$prefix/.bash_profile
	elif [ -f $prefix/.profile ]; then		
		profileFile=$prefix/.profile
	else
		profileFile=$prefix/.bashrc
		touch $prefix/.bashrc
	fi
		
	if [ ! -f ${profileFile}_backup ]; then
	    cp $profileFile ${profileFile}_backup
	fi

	egrep -qs 'MariaDB Columnstore Non-Root Environment Variables' ${profileFile}
	rc1=$?
	if [ $rc1 -ne 0 ]; then
        	echo " " >> ${profileFile}
		echo "# MariaDB Columnstore Non-Root Environment Variables" >> ${profileFile}
        	echo "export COLUMNSTORE_INSTALL_DIR=$COLUMNSTORE_INSTALL_DIR" >> ${profileFile}
        	echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$COLUMNSTORE_INSTALL_DIR/lib:$COLUMNSTORE_INSTALL_DIR/mysql/lib" >> ${profileFile}
	fi
	
	egrep -qs 'MariaDB Columnstore Non-Root Alias Variables' ${profileFile}
	rc1=$?
	if [ $rc1 -ne 0 ]; then
        	echo " " >> ${profileFile}
		echo "# MariaDB Columnstore Non-Root Alias Variables" >> ${profileFile}
		cat $installdir/bin/columnstoreAlias >> ${profileFile}
	fi

	#source the file
	. ${profileFile}
else
	profileFile="/etc/profile.d/columnstoreAlias.sh"
	/bin/cp -f $installdir/bin/columnstoreAlias /etc/profile.d/columnstoreAlias.sh
	chmod 644 /etc/profile.d/columnstoreAlias.sh >/dev/null 2>&1
fi

# set Profile File in config file
$installdir/bin/setConfig -d Installation ProfileFile $profileFile

# create directories
if [ $user == "root" ]; then
	test -d /var/log/mariadb || mkdir /var/log/mariadb >/dev/null 2>&1
	test -d /var/log/mariadb/columnstore || mkdir /var/log/mariadb/columnstore >/dev/null 2>&1
	test -d /var/log/mariadb/columnstore/archive || mkdir /var/log/mariadb/columnstore/archive >/dev/null 2>&1
	test -d /var/log/mariadb/columnstore/corefiles || mkdir /var/log/mariadb/columnstore/corefiles >/dev/null 2>&1
	test -d /var/log/mariadb/columnstore/trace || mkdir /var/log/mariadb/columnstore/trace >/dev/null 2>&1
	test -h /var/log/mariadb/columnstore/data && rm -f /var/log/mariadb/columnstore/data
	chmod 755 /var/log/mariadb/columnstore/corefiles > /dev/null 2>&1
fi

# make sure trace dir is world-writable and sticky
test -d $installdir/data || mkdir $installdir/data
test -d $installdir/data1 || mkdir $installdir/data1
test -d $installdir/data1/systemFiles || mkdir $installdir/data1/systemFiles
test -d $installdir/data1/systemFiles/dbrm || mkdir $installdir/data1/systemFiles/dbrm
test -d $installdir/data1/systemFiles/dataTransaction || rmdir $installdir/data1/systemFiles/dataTransaction >/dev/null 2>&1
test -d $installdir/data1/systemFiles/dataTransaction/archive || rmdir $installdir/data1/systemFiles/dataTransaction/archive >/dev/null 2>&1
chmod 1755 $installdir/data1 >/dev/null 2>&1
chmod -R 1755 $installdir/data1/systemFiles >/dev/null 2>&1
chmod 1755 $installdir/etc > /dev/null 2>&1

#create the bulk-load dirs
mkdir -p $installdir/data/bulk/data/import >/dev/null 2>&1
mkdir -p $installdir/data/bulk/log >/dev/null 2>&1
mkdir -p $installdir/data/bulk/job >/dev/null 2>&1
mkdir -p $installdir/data/bulk/rollback >/dev/null 2>&1
mkdir -p $installdir/data/bulk/tmpjob >/dev/null 2>&1
rm -f $installdir/data/bulk/tmpjob/* >/dev/null 2>&1

#get temp base directory
tmpDir="/tmp"
if [ $user != "root" ]; then
	tmpDir=$HOME"/.tmp"
	mkdir $tmpDir >/dev/null 2>&1
else
	#get columnstore temp file directory name
	TempFileDir=`$installdir/bin/getConfig SystemConfig TempFileDir`
	tmpDir=${tmpDir}${TempFileDir}
	mkdir $tmpDir >/dev/null 2>&1
fi

#set base columnstore temp file directory
$installdir/bin/setConfig -d SystemConfig SystemTempFileDir $tmpDir

#get place file buffer temporary files
hdfsRdwrScratch=`$installdir/bin/getConfig SystemConfig hdfsRdwrScratch`
hdfsDir=${tmpDir}${hdfsRdwrScratch}

#create place file buffer temporary files
mkdir -p $hdfsDir >/dev/null 2>&1

#create mount directories
mkdir /mnt/tmp > /dev/null 2>&1

# remove mysql archive log
test -d $installdir/mysql/db || mkdir -p $installdir/mysql/db
rm -rf $installdir/mysql/db/columnstore_log_archive > /dev/null 2>&1

# delete Columnstore shared memory segments
$installdir/bin/clearShm  > /dev/null 2>&1

#check and create rc.local file if missing
if [ -f /etc/rc.d ]; then
    RCFILE=/etc/rc.d/rc.local
else
    RCFILE=/etc/rc.local
fi

if [ $user = "root" ]; then
    touch $RCFILE    
    chmod +x $RCFILE
else
    printf '%s\n' '#!/bin/bash' "#" | tee -a $RCFILE > /dev/null 2>&1
fi

if [ $user = "root" ]; then
	#setup the columnstore service script
	rm -f /etc/init.d/columnstore >/dev/null 2>&1
	rm -f /etc/init.d/mysql-Columnstore >/dev/null 2>&1
	rm -f /etc/default/columnstore

	systemctl=`which systemctl 2>/dev/null`
	if [ -n "$systemctl" ]; then

		chmod 644 $installdir/bin/columnstore.service
		cp $installdir/bin/columnstore.service /usr/lib/systemd/system/. >/dev/null 2>&1
		cp $installdir/bin/columnstore.service /lib/systemd/system/. >/dev/null 2>&1
		systemctl enable columnstore >/dev/null 2>&1
	else
		chkconfig=`which chkconfig 2>/dev/null`
		if [ -n "$chkconfig" ]; then

			cp $installdir/bin/columnstore /etc/init.d/. >/dev/null 2>&1
			chkconfig --add columnstore > /dev/null 2>&1
			chkconfig columnstore on > /dev/null 2>&1
		else
			cp $installdir/bin/columnstore /etc/init.d/. >/dev/null 2>&1
			updaterc=`which update-rc.d 2>/dev/null`
			if [ -n "$updaterc" ]; then

				update-rc.d columnstore defaults 99 > /dev/null 2>&1
			else
				echo ""
				echo "Package 'systemctl', 'chkconfig' or 'update-rc.d' not installed, contact your sysadmin if you want to setup to autostart for columnstore"	
			fi
		fi
	fi
fi

if [ $user = "root" ]; then
	$installdir/bin/syslogSetup.sh install > $tmpDir/syslog_install.log 2>&1

	#check if MariaDB Columnstore system logging was setup
	cat $tmpDir/syslog_install.log | grep 'No System Logging' >/dev/null 2>&1
	if [ $? -eq 0 ]; then
	    cat $tmpDir/syslog_install.log
	fi
else
	sed -i -e s@/usr/local/mariadb/columnstore@$installdir@g $installdir/bin/columnstoreLogRotate

	sed -i -e s@prefix=/home/guest@prefix=$prefix@g $installdir/bin/*

	chown $user:$user $installdir/etc/Columnstore.xml
	
cat <<EOD

NOTE: For non-root install, you will need to run the following commands as root user to
      setup the MariaDB ColumnStore System Logging

export COLUMNSTORE_INSTALL_DIR=$COLUMNSTORE_INSTALL_DIR
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
$installdir/bin/syslogSetup.sh --installdir=$installdir --user=$user install
      
      
EOD
fi

#determine lock file directory

# Lock directory for root user
lockdir='/var/lock/subsys'
if [ $user != "root" ];then
    # Lock directory for non-root user
    lockdir=$prefix/.lock
fi

$installdir/bin/setConfig -d Installation LockFileDirectory $lockdir

mkdir $lockdir >/dev/null 2>&1

rm -f $lockdir/mysql-Columnstore
rm -f $lockdir/columnstore

#backup copy of Alarm Config File
/bin/cp -f $installdir/etc/AlarmConfig.xml $installdir/etc/AlarmConfig.xml.installSave > /dev/null 2>&1

#check and get amazon env variables
aws=`which aws 2>/dev/null`
if [ -z "aws" ]; then
	$installdir/bin/MCSgetCredentials.sh >/dev/null 2>&1
fi

#setup hadoop
hadoop=`which hadoop 2>/dev/null`
if [ -z "$hadoop" ]; then
	#check for non-root user
	if [ $installdir != "/usr/local/mariadb/columnstore" -a $quiet -eq 0 ]; then
cat <<EOD
The next steps are:

If installing on a pm1 node using non-distributed install

export COLUMNSTORE_INSTALL_DIR=$COLUMNSTORE_INSTALL_DIR
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
$installdir/bin/postConfigure -i $installdir

If installing on a pm1 node using distributed install

export COLUMNSTORE_INSTALL_DIR=$COLUMNSTORE_INSTALL_DIR
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
$installdir/bin/postConfigure -i $installdir -d

If installing on a non-pm1 using the non-distributed option:

export COLUMNSTORE_INSTALL_DIR=$COLUMNSTORE_INSTALL_DIR
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
$installdir/bin/columnstore start

EOD
	else
cat <<EOD
The next step is:

If installing on a pm1 node using non-distributed install

$installdir/bin/postConfigure

If installing on a pm1 node using distributed install

$installdir/bin/postConfigure -d

If installing on a non-pm1 using the non-distributed option:

$installdir/bin/columnstore start


EOD
	fi
        exit 0
else
	chmod 755 $installdir/bin/setenv-hdfs-12
	chmod 755 $installdir/bin/setenv-hdfs-20

	. $installdir/bin/setenv-hdfs-20
	$installdir/bin/hdfsCheck $installdir/lib/hdfs-20.so > $tmpDir/hdfs-20-test.log 2>&1
	if [ $? -eq 0 ]; then
		#Passed set in config file
		$installdir/bin/setConfig -d SystemConfig DataFilePlugin $installdir/lib/hdfs-20.so
		$installdir/bin/setConfig -d SystemConfig DataFileEnvFile setenv-hdfs-20
	
		#check for non-root user
		if [ $installdir != "/usr/local/mariadb/columnstore" -a $quiet -eq 0 ]; then
cat <<EOD

If you are intending to install MariaDB Columnstore over Hadoop, the next steps are:

If installing on a pm1 node:

export COLUMNSTORE_INSTALL_DIR=$COLUMNSTORE_INSTALL_DIR
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
. $installdir/bin/setenv-hdfs-20
$installdir/bin/postConfigure -i $installdir

If installing on a non-pm1 using the non-distributed option:

export COLUMNSTORE_INSTALL_DIR=$COLUMNSTORE_INSTALL_DIR
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
$installdir/bin/columnstore start

If you are intending to install MariaDB Columnstore without Hadoop, the next steps are:

If installing on a pm1 node:

export COLUMNSTORE_INSTALL_DIR=$COLUMNSTORE_INSTALL_DIR
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
$installdir/bin/postConfigure -i $installdir

If installing on a non-pm1 using the non-distributed option:

export COLUMNSTORE_INSTALL_DIR=$COLUMNSTORE_INSTALL_DIR
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
$installdir/bin/columnstore start

EOD
		else
cat <<EOD

If you are intending to install MariaDB Columnstore over Hadoop, the next steps are:

. $installdir/bin/setenv-hdfs-20
$installdir/bin/postConfigure

If installing on a non-pm1 using the non-distributed option:

. $installdir/bin/setenv-hdfs-20
$installdir/bin/columnstore start

If you are intending to install MariaDB Columnstore without Hadoop, the next step is:

If installing on a pm1 node:

$installdir/bin/postConfigure

If installing on a non-pm1 using the non-distributed option:

$installdir/bin/columnstore start

EOD
		fi
	else
		. $installdir/bin/setenv-hdfs-12
		$installdir/bin/hdfsCheck $installdir/lib/hdfs-12.so > $tmpDir/hdfs-12-test.log 2>&1
		if [ $? -eq 0 ]; then
			#Passed set in config file
			$installdir/bin/setConfig -d SystemConfig DataFilePlugin $installdir/lib/hdfs-12.so
			$installdir/bin/setConfig -d SystemConfig DataFileEnvFile setenv-hdfs-12

			if [ $installdir != "/usr/local/mariadb/columnstore" -a $quiet -eq 0 ]; then
cat <<EOD

If you are intending to install MariaDB Columnstore over Hadoop, the next steps are:

If installing on a pm1 node:

export COLUMNSTORE_INSTALL_DIR=$COLUMNSTORE_INSTALL_DIR
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
. $installdir/bin/setenv-hdfs-12
$installdir/bin/postConfigure -i $installdir

If you are intending to install MariaDB Columnstore without Hadoop, the next steps are:

If installing on a pm1 node:

export COLUMNSTORE_INSTALL_DIR=$COLUMNSTORE_INSTALL_DIR
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
. $installdir/bin/setenv-hdfs-12
$installdir/bin/postConfigure -i $installdir

EOD
			else
cat <<EOD

If you are intending to install MariaDB Columnstore over Hadoop, the next steps are:

If installing on a pm1 node:

. $installdir/bin/setenv-hdfs-12
$installdir/bin/postConfigure

If installing on a non-pm1 using the non-distributed option:

. $installdir/bin/setenv-hdfs-12
$installdir/bin/columnstore start

If you are intending to install MariaDB Columnstore without Hadoop, the next step is:

If installing on a pm1 node:

$installdir/bin/postConfigure

If installing on a non-pm1 using the non-distributed option:

$installdir/bin/columnstore start

EOD
			fi
		else
cat <<EOD

If you are intending to install MariaDB Columnstore over Hadoop, the Hadoop sanity check did not pass.  
Most likely there is an environment setup conflict or the hdfs services are down.
Please Contact MariaDB Columnstore Customer Support.
EOD
		fi
	fi
fi

exit 0

